package com.tools.scanner.ui.qrcode.creator.impl

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.tools.scanner.databinding.FragmentCalendarInputBinding
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
import java.util.TimeZone

class CreateCalendarFragment : BaseCreateFragment() {

    private lateinit var mBinding: FragmentCalendarInputBinding

    private var mStartTimestamp: Long = 0L
    private var mEndTimestamp: Long = 0L

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentCalendarInputBinding.inflate(inflater, container, false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val calendar = Calendar.getInstance()
        setTimeContent(true, calendar)
        calendar.add(Calendar.HOUR_OF_DAY, 1)
        setTimeContent(false, calendar)

        mBinding.tvStartDate.setOnClickListener {
            showDatePickerDialog(true)
        }

        mBinding.tvEndDate.setOnClickListener {
            showDatePickerDialog(false)
        }
    }

    private fun setTimeContent(isStartTime: Boolean, calendar: Calendar) {
        val year = calendar.get(Calendar.YEAR)
        val month = String.format("%02d", calendar.get(Calendar.MONTH) + 1)
        val day = String.format("%02d", calendar.get(Calendar.DAY_OF_MONTH))
        val hour = String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY))
        val minute = String.format("%02d", calendar.get(Calendar.MINUTE))

        val content = "${year}-${month}-${day} ${hour}:${minute}"

        if (isStartTime) {
            mStartTimestamp = calendar.timeInMillis
            mBinding.tvStartDate.text = content
        } else {
            mEndTimestamp = calendar.timeInMillis
            mBinding.tvEndDate.text = content
        }
    }

    private fun showDatePickerDialog(isStartTime: Boolean) {
        val calendar = Calendar.getInstance()

        if (isStartTime) {
            calendar.timeInMillis = mStartTimestamp
        } else {
            calendar.timeInMillis = mEndTimestamp
        }

        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)

        val datePickerDialog =
            DatePickerDialog(requireActivity(), { _, selectedYear, selectedMonth, selectedDay ->
                calendar.set(selectedYear, selectedMonth, selectedDay)
                showTimePickerDialog(isStartTime, calendar)
            }, year, month, day)
        datePickerDialog.show()
    }

    private fun showTimePickerDialog(isStartTime: Boolean, calendar: Calendar) {
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)

        val timePickerDialog =
            TimePickerDialog(requireActivity(), { _, selectedHour, selectedMinute ->
                calendar.set(Calendar.HOUR_OF_DAY, selectedHour)
                calendar.set(Calendar.MINUTE, selectedMinute)
                setTimeContent(isStartTime, calendar)
            }, hour, minute, false)
        timePickerDialog.show()
    }

    override fun createQrCodeContent(): String {
        val dateFormat = SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'", Locale.US)
        dateFormat.timeZone = TimeZone.getTimeZone("UTC")
        val startDate = dateFormat.format(Date(mStartTimestamp))
        val endDate = dateFormat.format(Date(mEndTimestamp))

        val content = """
            BEGIN:VCALENDAR
            VERSION:2.0
            BEGIN:VEVENT
            SUMMARY:${mBinding.etName.text}
            DTSTART:${startDate}
            DTEND:${endDate}
            LOCATION:${mBinding.etLocation.text}
            DESCRIPTION:${mBinding.etDest.text}
            END:VEVENT
            END:VCALENDAR
        """.trimIndent()

        return content
    }

    override fun checkParams(): Boolean {
        return true
    }

}